
//显示弹出选项框
function showEditStepWin(){
	return false;
}

//加载阶段
function loadStep(){
	$("#stepRow").html("");
	Otod.ajax({//查询已存在列表
		url : ctx + "/pm/stepTp/list.do",
		data : tpId,
		successFn:function(data){
			$.each(data,function(i,o){
				createStepWrap("stepRow",o.uuid,o.stepName,o.taskList);
			});
		}
	});
}

/*function loadStep(){
	$("#stepRow").html("");
	Otod.ajax({					//查询已存在列表
		url : ctx + "/pm/stepTp/list.do",
		data : tpId,
		successFn:function(data){
			$.each(data,function(i,o){
				createStepWrap("stepRow",o.uuid,o.stepName,o.taskList);
			});
		}
	});
}*/



$(function(){
	$('#stepModen-form').bootstrapValidator();
	$('#tpModen-form').bootstrapValidator();
	
	if(tpId==""){
		showTpModal($("#addTpModen"));
	}else{
		loadStep();		//页面启动后 加载阶段
	}
	
	
	
	$(".widget-toolbar").on('click',"a:has(.icon-edit)",function(e){
		e.preventDefault();
		showModal($(this),$("#stepModen"));
		
	});

	
	
	//任务新增按钮点击事件
	$(".step-content").on('click','.task-add-span',function(e){
		createTaskAddWrap($(this).attr("key"));
		
		return false;
	});
	
	//人员选择下拉
	$(".step-content").on('click',".emp-dropdown-toggle ~ ul > li a",function(e){
		var jVal = $(this).closest(".dropdown-menu").siblings(".emp-dropdown-toggle").children("span");
		jVal.text($(this).children("span").text());
	});
	
	//阶段保存
	$("#stepModen-btn-save").on('click',function(){
		if(!$('#stepModen-form').data('bootstrapValidator').validate().isValid()){
			return;
		}
		Otod.ajax({
			url: ctx + '/pm/stepTp/save.do',
			data : $('#stepModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				$('#stepModen').modal('hide');
				
				createStepWrap("stepRow",data.uuid,data.stepName);
			}
		});
	});
	
	//模板保存
	$("#tpModen-btn-save").on('click',function(){
		if(!$('#tpModen-form').data('bootstrapValidator').validate().isValid()){
			return;
		}
		Otod.ajax({
			url: ctx + '/pm/tp/save.do',
			data : $('#tpModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				/*
				$("input[name=tpId]").val(data.uuid);
				tpId = data.uuid;
				loadStep();
				$('#addTpModen').modal('hide');
				*/
				//修改为直接页跳转
				window.location.href = ctx+"/jsp/pm/prj/stepTp.jsp?tpId="+data.uuid;
			}
		});
	});
	
	$("#resetLoad").click(function(){
		
		loadStep($("input[name=tpId]").val());
	});
	
	
	//任务新增保存
	$(".step-content").on('click',"#task-btn-save",function(e){
		Otod.ajax({
			url: ctx + '/pm/taskTp/insert.do',
			data : $('#task-save-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				appendTaskItem(data);
				removeAddTaskWrap();
			}
		});
	});
	
	//任务新增取消
	$(".step-content").on('click',"#task-btn-cancle",function(e){
		removeAddTaskWrap();
	});
	
	//任务删除
	$(".step-content").on('click',".item-btn-delete",function(e){
		e.preventDefault();
		var taskId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该任务吗？", "删除任务", deleteTask);
		function deleteTask(){
			Otod.ajax({
				url: ctx + '/pm/taskTp/delete.do',
				data : taskId,
				successFn : function(data) {
					Otod.Msg.info("删除成功!");
					$("#task-item-panel-"+taskId).remove();
				}
			});
		}
	});
	
	//任务修改
	$(".step-content").on('click',".item-btn-edit",function(e){
		e.preventDefault();
		editTask(this);
	});
	

	//任务修改保存
	$("#taskModen-btn-save").on('click',function(){
		Otod.ajax({
			url: ctx + '/pm/taskTp/update.do',
			data : $('#taskModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				$('#taskModen').modal('hide');
				$('#task-item-panel-'+data.uuid + " label span").text(data.taskName);
			}
		});
	});
	
	//阶段删除
	$(".step-content").on('click',".step-btn-delete",function(e){
		e.preventDefault();
		var taskId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该阶段吗？", "删除阶段", deleteStep);
		function deleteStep(){
			Otod.ajax({
				url: ctx + '/pm/stepTp/delete.do',
				data : taskId,
				successFn : function(data) {
					Otod.Msg.info("删除成功!");
					$("#step-wrap-"+taskId).remove();
				}
			});
		}
		
		
	});
	
	//阶段修改
	$(".step-content").on('click',".step-btn-edit",function(e){
		e.preventDefault();
		editStep(this);
	});
	
});


//跳转到列表
function toTpList(){
	window.location.href = ctx + "/jsp/pm/prj/tp.jsp";
}


//新增step
function addStep(btn){
	resetStepModen();
	
	showModal(btn,"stepModen");
}

//修改step
function editStep(btn){
	resetStepModen();
	
	$("#stepModen [name='uuid']").val($(btn).attr("key"));
	$("#stepModen [name='stepName']").val($(btn).attr("stepName"));
	
	showModal(btn,"stepModen");
}

//重置step moden
function resetStepModen(){
	$("#stepModen-form")[0].reset();
	$("#stepModen-form [name='tpId']").val(tpId);		//初始化tpId
}

//显示modal
function showModal(btn,modal){
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	setModelFlowBtn(modalObj.children(".modal-dialog").get(0),btn);
}

//右侧显示modal
function showModalFollowRight(btn,modal){
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	setModelFollowRight(modalObj,"500px");
}

//显示modal
function showTpModal(modal){
	var modalObj = getJqueryObj(modal);
	modalObj.modal();
}



//修改step
function editTask(btn){
	resetTaskModen();
	var uuid = $(btn).attr("key");

	Otod.ajax({
		url: ctx + '/pm/taskTp/getById.do',
		data : uuid,
		successFn : function(data) {
			$("#taskModen [name='uuid']").val(data.uuid);
			$("#taskModen [name='tpId']").val(data.tpId);
			$("#taskModen [name='stepTpId']").val(data.stepTpId);
			$("#taskModen [name='taskName']").val(data.taskName);
			$("#taskModen [name='taskType']").val(data.taskType);
			
			
			showModalFollowRight(btn,"taskModen");
		}
	});
}

//重置step moden
function resetTaskModen(){
	$("#taskModen-form")[0].reset();
	$("#taskModen-form [name=tpId]").val(tpId);		//初始化tpId
}



/*小组件*******************************************/


//创建阶段列表包
function createStepWrap(appendTo,stepId,stepName,taskArray){
	var stepMtp = $('#stepMtp').html();
	Mustache.parse(stepMtp);
	
	var taskItemMtp = $('#taskItemMtp').html();
	
	var rendered = Mustache.render(stepMtp, {stepId: stepId, stepName: stepName,taskArray: taskArray},{taskItemLink:taskItemMtp});
	$('#'+appendTo).append(rendered);
}

//删除任务新增页面
function removeAddTaskWrap(){
	$(".item-task-add").remove();
}


//创建任务新增页面
function createTaskAddWrap(stepId){
	removeAddTaskWrap();
	var taskAddMtp = $('#taskAddMtp').html();
	Mustache.parse(taskAddMtp);
	var rendered = Mustache.render(taskAddMtp, {stepId: stepId,tpId:tpId});
	
	$("#step-wrap-ul-"+stepId).append(rendered);
	
	//$("#tagsSelect").appendTo("#divTaskType");			//加载下拉
	$("#divTaskType").append($("#tagsSelect").html());
}


//添加任务 item
function appendTaskItem(taskObj){
	var html = createTaskItemHtml(taskObj);
	$("#step-wrap-ul-"+taskObj.stepTpId).append(html);
}
//生成任务 item string
function createTaskItemHtml(taskObj){
	var template = $('#taskItemMtp').html();
	Mustache.parse(template);
	var rendered = Mustache.render(template, taskObj);
	return rendered;
}

